package com.five.community.controller;

import com.five.community.bean.Asset;
import com.five.community.bean.Building;
import com.five.community.bean.Village;
import com.five.community.bean.vo.AssetVo;
import com.five.community.bean.vo.BuildingVo;
import com.five.community.service.BuildingService;
import com.five.community.service.VillageService;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.Date;
import java.util.List;

@Controller
@RequestMapping("/building")
public class BuildingController {
    @Autowired
    private BuildingService buildingService;
    @Autowired
    private VillageService villageService;
    @RequestMapping("/listUi")
    public String listUi(@RequestParam(value = "pageNum",required = false,defaultValue = "1")Integer pageNum,
                         @RequestParam(value = "pageSize",required = false,defaultValue = "5")Integer pageSize,
                         Model model, ModelMap map){
        try {
            List<BuildingVo> pages= buildingService.selectAllBuildings(pageNum,pageSize);
            PageInfo<BuildingVo> pageInfo = new PageInfo<>(pages);
            model.addAttribute("pageInfo",pageInfo);
            List<BuildingVo> buildings = buildingService.selectAll();
            map.addAttribute("buildings",buildings);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }

        return "building";
    }
    //添加画面
    @RequestMapping("/addUi")
    public String addUi(ModelMap map1){
        try {
            List<Village> villages = villageService.selectAll();
            map1.addAttribute("villages",villages);

        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        return "building_add";
    }
    //添加方法
    @RequestMapping("/add")
    public String add(BuildingVo buildingVo){
        try {
            long vid = Long.parseLong(buildingVo.getVname());
            buildingVo.setCreateTime(new Date());
            Building building = new Building(buildingVo.getId(),vid,buildingVo.getSerialNumber(),buildingVo.getName(),buildingVo.getHouses(),
           buildingVo.getDescribes(),new Date() );
            buildingService.insert(building);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "redirect:/building/addUi";
    }
    //删除方法
    @RequestMapping("/delete")
    public String delete(long id){
        try {
            buildingService.deleteById(id);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        return "redirect:/building/listUi";
    }
    //查询方法
    @RequestMapping("/select")
    public String select(@RequestParam(value = "pageNum",required = false,defaultValue = "1")Integer pageNum,
                         @RequestParam(value = "pageSize",required = false,defaultValue = "5")Integer pageSize,
                         String name,ModelMap map,Model model) {
        if (name == "") {
            return "redirect:/building/listUi";
        } else {
            try {
                List<BuildingVo> pages= buildingService.selectAllByName(pageNum,pageSize,name);
                List<BuildingVo> buildings = buildingService.selectByName(name);
                PageInfo<BuildingVo> pageInfo = new PageInfo<>(pages);
                model.addAttribute("pageInfo",pageInfo);
                map.addAttribute("buildings", buildings);
            } catch (Exception e) {
                throw new RuntimeException(e);
            }
            return "building";
        }
    }
    //更新页面
    @RequestMapping("/editUi")
    public String editUi(long id, ModelMap map1){
        try {
           Building building = buildingService.selectById(id);
            List<Village> villages = villageService.selectAll();
            map1.addAttribute("villages",villages);
            map1.addAttribute("building",building);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        return "building_edit";
    }
    //更新方法
    @RequestMapping("/edit")
    public String edit(BuildingVo buildingVo) {
        long vid = Long.parseLong(buildingVo.getVname());
        buildingVo.setCreateTime(new Date());
        try {
            Building building = new Building(buildingVo.getId(),vid,buildingVo.getSerialNumber(),buildingVo.getName(),buildingVo.getHouses(),
                    buildingVo.getDescribes(),new Date());
            buildingService.update(building);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        return "building_edit";
    }
}
